/*
 * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef _IOT_BOARD_H_
#define _IOT_BOARD_H_

#include "esp_err.h"
#include "board_common.h"

/**
 * Resource ID on Board,
 * You can use iot_board_get_handle(ID,handle) to reference Resource's Handle
 * */
typedef enum {
    NULL_RESOURCE,
    BOARD_I2C0_ID,
    BOARD_SPI2_ID,
} board_res_id_t;

/*Definitions of Board Information*/
#define BOARD_NAME "PDU-S3"
#define BOARD_VENDOR "CHENGVIN"
#define BOARD_URL "https://www.myainet.com"

/*Definitions of MCU Information*/
#define BOARD_MCU_NAME "ESP32S3"
#define BOARD_MODULE_NAME "ESP32-S3-WROOM"
#define BOARD_MCU_FLASH_SIZE "4MB"
#define BOARD_MCU_RAM_SIZE "512KB"
#define BOARD_MCU_RAM_USER_SIZE "384KB"

#define BOARD_IO_BASE_XL9535 50  // 为了避免和ESP32的IO冲突，XL9535的IO编号从50开始
#define BOARD_IO_BASE_XL9535_2 (BOARD_IO_BASE_XL9535+20)  // 第二路XL9535的IO编号从70开始

/*Definitions of Button*/
#define BOARD_BUTTON_NUM 9
#define BOARD_IO_BUTTON_1 (0+BOARD_IO_BASE_XL9535)
#define BOARD_IO_BUTTON_2 (1+BOARD_IO_BASE_XL9535)
#define BOARD_IO_BUTTON_3 (2+BOARD_IO_BASE_XL9535)
#define BOARD_IO_BUTTON_4 (3+BOARD_IO_BASE_XL9535)
#define BOARD_IO_BUTTON_5 (4+BOARD_IO_BASE_XL9535)
#define BOARD_IO_BUTTON_6 (5+BOARD_IO_BASE_XL9535)
#define BOARD_IO_BUTTON_7 (1+BOARD_IO_BASE_XL9535_2)
#define BOARD_IO_BUTTON_8 (14+BOARD_IO_BASE_XL9535_2)
#define BOARD_IO_BUTTON_9 (15+BOARD_IO_BASE_XL9535_2)

#define BOARD_IO_DI_1 BOARD_IO_BUTTON_1
#define BOARD_IO_DI_2 BOARD_IO_BUTTON_2
#define BOARD_IO_DI_3 BOARD_IO_BUTTON_3
#define BOARD_IO_DI_4 BOARD_IO_BUTTON_4
#define BOARD_IO_CHG_STA BOARD_IO_BUTTON_5
#define BOARD_IO_CHG_BY BOARD_IO_BUTTON_7
#define BOARD_IO_USER_KEY BOARD_IO_BUTTON_6
#define BOARD_IO_RTC_INT BOARD_IO_BUTTON_8
#define BOARD_IO_E32_INT BOARD_IO_BUTTON_9

/*Definitions of LED*/
#define BOARD_LED_NUM 14
#define BOARD_LED_TYPE_1 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_2 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_3 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_4 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_5 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_6 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_7 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_8 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_9 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_10 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_11 LED_TYPE_GPIO
#define BOARD_LED_TYPE_12 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_13 LED_TYPE_I2C_EXIO
#define BOARD_LED_TYPE_14 LED_TYPE_I2C_EXIO

#define BOARD_LED_POLARITY_1 _NEGATIVE
#define BOARD_LED_POLARITY_5 _NEGATIVE
#define BOARD_LED_POLARITY_6 _NEGATIVE
#define BOARD_LED_POLARITY_7 _NEGATIVE
#define BOARD_LED_POLARITY_8 _NEGATIVE

#define BOARD_IO_LED_1 (BOARD_IO_BASE_XL9535+6)
#define BOARD_IO_LED_2 (BOARD_IO_BASE_XL9535+7)
#define BOARD_IO_LED_3 (BOARD_IO_BASE_XL9535+8)
#define BOARD_IO_LED_4 (BOARD_IO_BASE_XL9535+9)
#define BOARD_IO_LED_5 (BOARD_IO_BASE_XL9535+10)
#define BOARD_IO_LED_6 (BOARD_IO_BASE_XL9535+11)
#define BOARD_IO_LED_7 (BOARD_IO_BASE_XL9535+12)
#define BOARD_IO_LED_8 (BOARD_IO_BASE_XL9535+13)
#define BOARD_IO_LED_9 (BOARD_IO_BASE_XL9535+14)
#define BOARD_IO_LED_10 (BOARD_IO_BASE_XL9535+15)
#define BOARD_IO_LED_11 15
#define BOARD_IO_LED_12 (BOARD_IO_BASE_XL9535_2+5)
#define BOARD_IO_LED_13 (BOARD_IO_BASE_XL9535_2+8)
#define BOARD_IO_LED_14 (BOARD_IO_BASE_XL9535_2+9)


#define BOARD_IO_LOWBT_LED BOARD_IO_LED_1
#define BOARD_IO_4GPOW_EN BOARD_IO_LED_2
#define BOARD_IO_5VPOW_EN BOARD_IO_LED_3
#define BOARD_IO_LoRaLED BOARD_IO_LED_4
#define BOARD_IO_DO_1 BOARD_IO_LED_5
#define BOARD_IO_DO_2 BOARD_IO_LED_6
#define BOARD_IO_DO_3 BOARD_IO_LED_7
#define BOARD_IO_DO_4 BOARD_IO_LED_8
#define BOARD_IO_BZ BOARD_IO_LED_9
#define BOARD_IO_ML307R_REST BOARD_IO_LED_10
#define BOARD_IO_AMP_POW_EN BOARD_IO_LED_12
#define BOARD_IO_E32_MD0 BOARD_IO_LED_14
#define BOARD_IO_E32_MD1 BOARD_IO_LED_13

/* IO I2C0 */
#define BOARD_IO_I2C0_SCL 13
#define BOARD_IO_I2C0_SDA 12

/* IO SPI2 */
#define BOARD_IO_SPI2_SCK 36
#define BOARD_IO_SPI2_MOSI 35
#define BOARD_IO_SPI2_MISO 37

/* Free pins - left */
#define BOARD_IO_FREE_4 4
#define BOARD_IO_FREE_5 5
#define BOARD_IO_FREE_6 6
#define BOARD_IO_FREE_7 7
#define BOARD_IO_FREE_15 15
#define BOARD_IO_FREE_16 16
#define BOARD_IO_FREE_17 17
#define BOARD_IO_FREE_18 18
#define BOARD_IO_FREE_8 8
#define BOARD_IO_FREE_3 3
#define BOARD_IO_FREE_46 46
#define BOARD_IO_FREE_9 9
// #define BOARD_IO_FREE_10 10
// #define BOARD_IO_FREE_11 11
#define BOARD_IO_FREE_12 12
#define BOARD_IO_FREE_13 13
#define BOARD_IO_FREE_14 14

/* Free pins - right */
#define BOARD_IO_FREE_43 43
#define BOARD_IO_FREE_44 44
#define BOARD_IO_FREE_1 1
#define BOARD_IO_FREE_2 2
#define BOARD_IO_FREE_42 42
#define BOARD_IO_FREE_41 41
#define BOARD_IO_FREE_40 40
#define BOARD_IO_FREE_39 39
#define BOARD_IO_FREE_38 38
// #define BOARD_IO_FREE_37 37
// #define BOARD_IO_FREE_36 36
// #define BOARD_IO_FREE_35 35
// #define BOARD_IO_FREE_0 0
#define BOARD_IO_FREE_45 45
#define BOARD_IO_FREE_48 48
#define BOARD_IO_FREE_47 47
#define BOARD_IO_FREE_21 21
// #define BOARD_IO_FREE_20 20
// #define BOARD_IO_FREE_19 19

//USB Configuration (fixed)
#define BOARD_IO_USB_DP 20
#define BOARD_IO_USB_DN 19

#define UART_PORT_485           (1)
#define GPIO_485_TXD            (1)
#define GPIO_485_RXD            (2)
#define GPIO_485_DIR            (45)

#define UART_PORT_485_2           (2)
// board v0.90
// #define GPIO_485_2_TXD            (16)  
// #define GPIO_485_2_RXD            (17)
// board v0.91
#define GPIO_485_2_TXD            (39)
#define GPIO_485_2_RXD            (38)
#define GPIO_485_2_DIR            (46)

#define UART_PORT_RF           (2)
#define GPIO_RF_TXD            (40)
#define GPIO_RF_RXD            (41)

#define BOARD_IO_POWIN_DEC (18)
#define GPIO_ETIO0_INT (16)
#define GPIO_ETIO0_INT (17)

#define BOARD_IO_PIN_SEL_INPUT ( (1ULL<<BOARD_IO_POWIN_DEC) | (1ULL<<GPIO_ETIO0_INT) | (1ULL<<GPIO_ETIO0_INT) )

#endif /* _IOT_BOARD_H_ */
